﻿namespace Digitalmes.Common.Utils;

/// <summary>
/// 时间操作
/// </summary>
public static class TimeUtils
{
    /// <summary>
    /// 分割时间，查询作用
    /// </summary>
    /// <param name="timeStr"></param>
    /// <param name="split"></param>
    /// <returns></returns>
    public static (string beginTime, string endTime) Splitting(string timeStr, char split = '/')
    {
        var time = timeStr.Split(new char[] { split }, StringSplitOptions.RemoveEmptyEntries);
        return (time[0], time[1]);
    }

    /// <summary>
    /// 获取当前时间戳，精确到秒。
    /// </summary>
    /// <returns></returns>
    public static long GetTimeStamp()
    {
        return DateTimeOffset.Now.ToUnixTimeSeconds();
    }

    /// <summary>
    /// 获取当前时间戳，精确到毫秒。
    /// </summary>
    /// <returns></returns>
    public static long GetTimeStamp2()
    {
        return DateTimeOffset.Now.ToUnixTimeMilliseconds();
    }

    /// <summary>
    /// 获取指定时间的时间戳，精确到秒。
    /// </summary>
    /// <param name="dateTime">指定的时间</param>
    /// <returns></returns>
    public static long GetTimeStamp(DateTime dateTime)
    {
        return new DateTimeOffset(dateTime).ToUnixTimeSeconds();
    }

    /// <summary>
    /// 获取指定时间的时间戳，精确到毫秒。
    /// </summary>
    /// <param name="dateTime">指定的时间</param>
    /// <returns></returns>
    public static long GetTimeStamp2(DateTime dateTime)
    {
        return new DateTimeOffset(dateTime).ToUnixTimeMilliseconds();
    }
}
